package cn.wangzg.course0602.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import cn.wangzg.course0602.LoginActivity;

/**
 * Time: 2020/3/31
 * Author: wangzhiguo
 * Description: 功能描述
 */
public class HttpUtil {
    static CookieManager manager = new CookieManager();
    static {
        CookieHandler.setDefault(manager);
    }
    //2.网络请求
    /**
     * 发送Get请求
     * @param strUrl 请求地址
     * @return  返回String
     */
    public static String doGet(String strUrl){
        String ret=null;
        try {
            URL url = new URL(strUrl);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            //conn.setRequestMethod("GET");//默认，可以省略
            conn.setConnectTimeout(5000);
            InputStream is = conn.getInputStream();
            ret=is2String(is);
            conn.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }
    public static Bitmap doGetBitmap(String strUrl){
        Bitmap ret=null;
        try {
            URL url = new URL(strUrl);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            //conn.setRequestMethod("GET");//默认，可以省略
            conn.setConnectTimeout(5000);
            InputStream is = conn.getInputStream();
            ret= BitmapFactory.decodeStream(is);
            conn.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 将流转为字符串
     * @param inStream  输入流
     * @return  返回字符串
     * @throws IOException
     */
    private static String is2String(InputStream inStream) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len = inStream.read(buffer)) != -1)
        {
            outStream.write(buffer,0,len);
        }
        inStream.close();
        return new String(outStream.toByteArray(), "UTF-8");
    }
    private static String is2String2(InputStream inStream) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(inStream,"utf-8"));
        StringBuffer sb=new StringBuffer();
        String line;
        while ((line=br.readLine())!=null){
            sb.append(line);
        }
        br.close();
        inStream.close();
        return sb.toString();
    }

    public static String doPost(String strUrl,String data){
        try {
            URL url = new URL(strUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            //--------
            conn.setRequestMethod("POST");

            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", data.length() + "");
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            os.write(data.getBytes());
            os.flush();//清除缓冲
            //-----------
            int code = conn.getResponseCode();
            if (code == 200) {
                InputStream is = conn.getInputStream();
                return is2String(is);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
